package com.bawei.property.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.property.R
import com.bawei.property.databinding.ItemParkBinding
import com.bawei.property.entity.ParkValue

/**
 * Author: Jason Zhang
 * Date: 2024/8/28
 * Desc: 车位列表数据适配器
 */
class ParkListAdapter  constructor(private val list: MutableList<ParkValue>): RecyclerView.Adapter<ParkListAdapter.ItemViewHolder>(){

    private var mListener: ItemParkOnClickListener? = null

    inner class ItemViewHolder(val itemDataBinding: ItemParkBinding): RecyclerView.ViewHolder(itemDataBinding.root)

    interface ItemParkOnClickListener{
        fun onClick(v: View, position: Int)
    }

    fun setOnItemClickListener(listener: ItemParkOnClickListener){
        this.mListener = listener
    }

    override fun getItemCount(): Int = list.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        var itemDataBinding = DataBindingUtil.inflate<ItemParkBinding>(LayoutInflater.from(parent.context), R.layout.item_park, parent, false)
        return ItemViewHolder(itemDataBinding)
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        var valueEntity = list[position]

        //设置不同颜色
        when(valueEntity.parkUser){
            -1 -> {
                holder.itemDataBinding.tvParkName.setTextColor(Color.BLACK)
                holder.itemDataBinding.tvParkName.setBackgroundColor(Color.WHITE)
                holder.itemDataBinding.tvParkName.text = valueEntity.parkName
            }
            0 -> {
                holder.itemDataBinding.tvParkName.setBackgroundColor(Color.parseColor("#8fdb0a"))
                holder.itemDataBinding.tvParkName.text = "空位"
            }
            else -> {
                holder.itemDataBinding.tvParkName.text = valueEntity.parkName
                holder.itemDataBinding.tvParkName.setBackgroundColor(Color.parseColor("#CCCCCC"))
            }
        }

        //添加点击事件
        holder.itemDataBinding.tvParkName.setOnClickListener {
            if (valueEntity.parkUser != -1){
                mListener?.onClick(it, position)
            }
        }

    }


}